package SA.teacher.manage;

import java.util.ArrayList;

import SA.teacher.R;
import SA.teacher.R.drawable;
import SA.teacher.R.id;
import SA.teacher.R.layout;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<ManageData> arrData;
	private LayoutInflater inflater;
	boolean attendBtnClick;
	boolean hwBtnClick;

	public MyAdapter(Context c, ArrayList<ManageData> arr) {
		this.context = c;
		this.arrData = arr;
		inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return arrData.size();
	}

	public String getItem(int position) {
		return arrData.get(position).getStudentName();
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.manage1_list, parent, false);
		}

		ImageView studentPic = (ImageView) convertView.findViewById(R.id.studentpic);
		studentPic.setImageResource(arrData.get(position).getStudentPic());

		TextView studentName = (TextView) convertView.findViewById(R.id.studentname);
		studentName.setText(arrData.get(position).getStudentName());
		studentName.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(context, ManageStudent.class);
				//intent.putExtra("classname",classes[which]);
				//intent.putExtra("param1", position);
				context.startActivity(intent);
			}});

		final ImageView attnedBtn = (ImageView) convertView.findViewById(R.id.attendbtn);
		attnedBtn.setImageResource(arrData.get(position).getAttendBtn());

		attendBtnClick = false;
		hwBtnClick = false;


		attnedBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (attendBtnClick == true) {
					attnedBtn.setImageResource(R.drawable.roundbtn);
					attendBtnClick = false;

				} else {
					attnedBtn.setImageResource(R.drawable.roundbtnp);
					attendBtnClick = true;
				}
			}

		});

		final ImageView hwBtn = (ImageView) convertView.findViewById(R.id.hwbtn);
		hwBtn.setImageResource(arrData.get(position).getHwBtn());
		hwBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (hwBtnClick) {
					hwBtn.setImageResource(R.drawable.roundbtn);
					hwBtnClick = false;

				} else {
					hwBtn.setImageResource(R.drawable.roundbtnp);
					hwBtnClick = true;
				}
			}
		});

		return convertView;
	}

}
